package presentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TreePopUpMenu extends JPopupMenu implements ActionListener {

    TableFilteringScreen dbFileterScreen;
    TreePath path;
    DefaultMutableTreeNode treeNode;
    
    JMenuItem addFilter;
    String addFilterString = "Add New Filter";
    
    JMenuItem showResult;
    String showResultString = "Show Result in Table";
    
    JMenuItem deleteFilter;
    String deleteFilterString = "Delete Selected Filter";
    
    JMenuItem expandAll;
    String expandAllString = "Expand All";
    
    JMenuItem collapseAll;
    String collapseAllString = "Collapse All";
    
    JMenuItem csvExport;
    String csvExportString = "Export Result to CSV";
    
    JMenuItem xmlExport;
    String xmlExportString = "Export Result to XML";
    
    JMenuItem editFilter;
    String editFilterString = "Edit Selected Filter";

    public TreePopUpMenu(TableFilteringScreen dbFileterScreen, TreePath path) {
        
        this.dbFileterScreen = dbFileterScreen;
        this.path = path;
        this.treeNode = (DefaultMutableTreeNode) path.getLastPathComponent();

        addFilter = new JMenuItem(addFilterString);
        addFilter.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon addIcon = new ImageIcon(PresentationConfig.plusIconPath);
        addFilter.setIcon(addIcon);
        add(addFilter);

        deleteFilter = new JMenuItem(deleteFilterString);
        deleteFilter.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon crossIcon = new ImageIcon(PresentationConfig.crossIconPath);
        deleteFilter.setIcon(crossIcon);
        add(deleteFilter);
        if (treeNode.isRoot()) {
            deleteFilter.setEnabled(false);
        }
        
        editFilter = new JMenuItem(editFilterString);
        editFilter.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon editIcon = new ImageIcon(PresentationConfig.editIconPath);
        editFilter.setIcon(editIcon);
        add(editFilter); 
        if(treeNode.isRoot()){
            editFilter.setEnabled(false);
        }

        this.addSeparator();

        expandAll = new JMenuItem(expandAllString);
        expandAll.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        add(expandAll);
        if (treeNode.getChildCount() == 0) {
            expandAll.setEnabled(false);
        }

        collapseAll = new JMenuItem(collapseAllString);
        collapseAll.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        add(collapseAll);
        if (treeNode.getChildCount() == 0) {
            collapseAll.setEnabled(false);
        }

        this.addSeparator();

        csvExport = new JMenuItem(csvExportString);
        csvExport.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon csvIcon = new ImageIcon(PresentationConfig.excelIconPath);
        csvExport.setIcon(csvIcon);
        add(csvExport);

        xmlExport = new JMenuItem(xmlExportString);
        xmlExport.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon xmlIcon = new ImageIcon(PresentationConfig.xmlIconPath);
        xmlExport.setIcon(xmlIcon);
        add(xmlExport);

        this.addSeparator();

        showResult = new JMenuItem(showResultString);
        showResult.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 12));
        Icon showResultIcon = new ImageIcon(PresentationConfig.showResultIconPath);
        showResult.setIcon(showResultIcon);
        add(showResult);

        initActionListeners();
        
      
    }

    private void initActionListeners() {
        showResult.addActionListener(this);
        addFilter.addActionListener(this);
        editFilter.addActionListener(this);
        deleteFilter.addActionListener(this);
        expandAll.addActionListener(this);
        collapseAll.addActionListener(this);
        csvExport.addActionListener(this);
        xmlExport.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e != null) {
            JMenuItem source = (JMenuItem) (e.getSource());
            if (source.equals(addFilter)) {
                dbFileterScreen.addFilter(path);
            } else if (source.equals(deleteFilter)) {
                dbFileterScreen.deleteFilter(path);
            } else if (source.equals(expandAll)) {
                dbFileterScreen.expandAll(path, true);
            } else if (source.equals(collapseAll)) {
                dbFileterScreen.expandAll(path, false);
            } else if (source.equals(showResult)) {
                dbFileterScreen.showTable(path);
            } else if (source.equals(csvExport)) {
                dbFileterScreen.exportToCSV(path);
            } else if (source.equals(xmlExport)) {
                dbFileterScreen.exportToXML(path);
            } else if(source.equals(editFilter)){
                dbFileterScreen.editFilter(path);
            }
        }
    }
}
